package com.tqz.jvm;

import com.sun.crypto.provider.DESKeyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import sun.misc.Launcher;

import java.net.URL;

/**
 * <p>类加载器测试。
 *
 * @author tianqingzhao
 * @since 2022/3/27 18:31
 */
public class JdkClassLoaderTest {
    
    private static Logger log = LoggerFactory.getLogger(JdkClassLoaderTest.class);
    
    public static void main(String[] args) {
        log.info("main方法開始，我是{}。", "tianqingzhao");
        log.error("错误日志");
        
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        AppConfig appConfig = applicationContext.getBean(AppConfig.class);
        System.out.println(appConfig.toString());
        
        // String是c++生成的对象，java是看不到的，所以为null
        System.out.println(String.class.getClassLoader());
        // 扩展类加载器 sun.misc.Launcher$ExtClassLoader@d041cf
        System.out.println(DESKeyFactory.class.getClassLoader());
        // 应用程序类加载器 sun.misc.Launcher$AppClassLoader@18b4aac2
        System.out.println(JdkClassLoaderTest.class.getClassLoader());
        
        ClassLoader appClassLoader = ClassLoader.getSystemClassLoader();
        ClassLoader extClassLoader = appClassLoader.getParent();
        ClassLoader bootstrapClassLoader = extClassLoader.getParent();
        // 系统类加载器 sun.misc.Launcher$AppClassLoader@18b4aac2
        System.out.println("appClassLoader:" + appClassLoader);
        // 扩展类加载器 sun.misc.Launcher$ExtClassLoader@28feb3fa
        System.out.println("extClassLoader:" + extClassLoader);
        // 引导类加载器 null
        System.out.println("bootstrapClassLoader:" + bootstrapClassLoader);
        
        System.out.println("boootstrap加载jar包======");
        URL[] urls = Launcher.getBootstrapClassPath().getURLs();
        for (URL url : urls) {
            // jdk安装目录的jre/lib里面的rt.jar、resources.jar、charsets.jar等等
            System.out.println(url);
        }
    
        System.out.println("extClassLoader加载======");
        // C:\Program Files\Java\jdk1.8.0_151\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
        System.out.println(System.getProperty("java.ext.dirs"));
    
        System.out.println("appClassLoader加载======");
        // 会读取当前项目所有的jar，包括jre的rt包，但是只会加载target下面的。
        System.out.println(System.getProperty("java.class.path"));
        
    }
    
}
